/*
 * RemoteDeliveryQueueLookupService.java
 *
 * Created on 6 June 2007, 13:06
 * Copyright (C) 2007   Yuezhong Zhang
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package usyd.comp5348;

import java.util.Hashtable;
import java.util.Properties;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Yuezhong Zhang
 */
public class RemoteDeliveryQueueLookupService {
    private final static RemoteDeliveryQueueLookupService instance = new RemoteDeliveryQueueLookupService();
    /**
     * Creates a new instance of RemoteDeliveryQueueLookupService
     */
    private Hashtable<String,String> table = new Hashtable<String,String>();
    public static RemoteDeliveryQueueLookupService getInstance(){
        return instance;
    }
    private RemoteDeliveryQueueLookupService() {
        /**
         * This is a warehousename and IP mapping, this can be loaded from a file
         */
        table.put("SydneyWarehouse1","192.168.10.5");
        table.put("SydneyWarehouse2","192.168.10.6");
        table.put("SydneyWarehouse3","192.168.10.7");
        table.put("SydneyWarehouse4","192.168.10.8");
    }
    
    /*
     * This isn't great.... should cache the ConnectionFactory objects, but time is at an end
     */
    public ConnectionFactory lookupConnectionFactory(String warehouse){
        try {
            Properties env1 = new Properties();
//            env1.setProperty("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
//            env1.setProperty("java.naming.provider.url", "iiop://"+table.get(warehouse) +":3700");
//            env1.setProperty("java.naming.factory.url.pkgs","com.sun.enterprise.naming");
//            env1.setProperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
//            env1.setProperty("org.omg.CORBA.ORBInitialHost",table.get(warehouse));
//            env1.setProperty("org.omg.CORBA.ORBInitialPort","3700");
            String addr = table.get(warehouse);
            if (addr==null)
                addr=warehouse; // raw IP?
            env1.put("java.naming.provider.url","iiop://"+ addr +":3700");
            // env1.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.appserv.naming.S1ASCtxFactory");
            env1.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.enterprise.naming.SerialInitContextFactory");
            InitialContext ic1 = new InitialContext( env1 );
            return(ConnectionFactory) ic1.lookup("jms/DeliveryMessageDrivenBeanFactory");
        } catch (NamingException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    public Queue getQueue(String warehouse){
        try {
            Properties env1 = new Properties();
//            env1.setProperty("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
//            env1.setProperty("java.naming.provider.url", "iiop://"+table.get(warehouse) +":3700");
//            env1.setProperty("java.naming.factory.url.pkgs","com.sun.enterprise.naming");
//            env1.setProperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
//            env1.setProperty("org.omg.CORBA.ORBInitialHost",table.get(warehouse));
//            env1.setProperty("org.omg.CORBA.ORBInitialPort","3700");
            String addr = table.get(warehouse);
            if (addr==null)
                addr=warehouse; // raw IP?
            env1.put("java.naming.provider.url","iiop://"+ addr+":3700");
            // env1.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.appserv.naming.S1ASCtxFactory");
            env1.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.enterprise.naming.SerialInitContextFactory");
            InitialContext ic1 = new InitialContext( env1 );
            return(Queue) ic1.lookup("jms/DeliveryMessageDrivenBean");
        } catch (NamingException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
